3 use MediaWiki\FileBackend\FSFile\TempFSFileFactory
;
6 * Location holder of files stored temporarily
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
24 * @ingroup FileBackend
27 use Wikimedia\AtEase\AtEase
;
30 * This class is used to hold the location and do limited manipulation
31 * of files stored temporarily (this will be whatever wfTempDir() returns)
33 * @ingroup FileBackend
35 class TempFSFile
extends FSFile
{
36 /** @var bool Garbage collect the temp file */
37 protected $canDelete = false;
39 /** @var array Map of (path => 1) for paths to delete on shutdown */
40 protected static $pathsCollect = null;
43 * Do not call directly. Use TempFSFileFactory
47 public function __construct( $path ) {
48 parent
::__construct( $path );
50 if ( self
::$pathsCollect === null ) {
51 // @codeCoverageIgnoreStart
52 self
::$pathsCollect = [];
53 register_shutdown_function( [ __CLASS__
, 'purgeAllOnShutdown' ] );
54 // @codeCoverageIgnoreEnd
59 * Make a new temporary file on the file system.
60 * Temporary files may be purged when the file object falls out of scope.
62 * @deprecated since 1.34, use TempFSFileFactory directly
64 * @param string $prefix
65 * @param string $extension Optional file extension
66 * @param string|null $tmpDirectory Optional parent directory
67 * @return TempFSFile|null
69 public static function factory( $prefix, $extension = '', $tmpDirectory = null ) {
70 return ( new TempFSFileFactory( $tmpDirectory ) )->newTempFSFile( $prefix, $extension );
74 * @todo Is there any useful way to test this? Would it be useful to make this non-static on
77 * @return string Filesystem path to a temporary directory
78 * @throws RuntimeException if no writable temporary directory can be found
80 public static function getUsableTempDirectory() {
81 $tmpDir = array_map( 'getenv', [ 'TMPDIR', 'TMP', 'TEMP' ] );
82 $tmpDir[] = sys_get_temp_dir();
83 $tmpDir[] = ini_get( 'upload_tmp_dir' );
84 foreach ( $tmpDir as $tmp ) {
85 if ( $tmp != '' && is_dir( $tmp ) && is_writable( $tmp ) ) {
90 // PHP on Windows will detect C:\Windows\Temp as not writable even though PHP can write to
91 // it so create a directory within that called 'mwtmp' with a suffix of the user running
92 // the current process.
93 // The user is included as if various scripts are run by different users they will likely
94 // not be able to access each others temporary files.
95 if ( strtoupper( substr( PHP_OS
, 0, 3 ) ) === 'WIN' ) {
96 $tmp = sys_get_temp_dir() . DIRECTORY_SEPARATOR
. 'mwtmp-' . get_current_user();
97 if ( !file_exists( $tmp ) ) {
100 if ( is_dir( $tmp ) && is_writable( $tmp ) ) {
105 throw new RuntimeException(
106 'No writable temporary directory could be found. ' .
107 'Please explicitly specify a writable directory in configuration.' );
111 * Purge this file off the file system
113 * @return bool Success
115 public function purge() {
116 $this->canDelete
= false; // done
117 AtEase
::suppressWarnings();
118 $ok = unlink( $this->path
);
119 AtEase
::restoreWarnings();
121 unset( self
::$pathsCollect[$this->path
] );
127 * Clean up the temporary file only after an object goes out of scope
129 * @param object $object
130 * @return TempFSFile This object
132 public function bind( $object ) {
133 if ( is_object( $object ) ) {
134 if ( !isset( $object->tempFSFileReferences
) ) {
135 // Init first since $object might use __get() and return only a copy variable
136 $object->tempFSFileReferences
= [];
138 $object->tempFSFileReferences
[] = $this;
145 * Set flag to not clean up after the temporary file
147 * @return TempFSFile This object
149 public function preserve() {
150 $this->canDelete
= false;
152 unset( self
::$pathsCollect[$this->path
] );
158 * Set flag clean up after the temporary file
160 * @return TempFSFile This object
162 public function autocollect() {
163 $this->canDelete
= true;
165 self
::$pathsCollect[$this->path
] = 1;
171 * Try to make sure that all files are purged on error
173 * This method should only be called internally
175 * @codeCoverageIgnore
177 public static function purgeAllOnShutdown() {
178 foreach ( self
::$pathsCollect as $path => $unused ) {
179 AtEase
::suppressWarnings();
181 AtEase
::restoreWarnings();
186 * Cleans up after the temporary file by deleting it
188 function __destruct() {
189 if ( $this->canDelete
) {